#include "app.h"
#include <uis.h>
#include "../dbg/dbg.h"

// TODO: set previous state here
#define BACK_STATE			NEWAPP_STATE_MAIN

// -----------------------------------------------------------------------------------------
// Local constants

static const WCHAR		str_view_title[] = {'T','i','t','l','e',0};
static const WCHAR		str_view_hw[] = {'H','e','l','l','o',' ','W','o','r','l','d','!',0};


// -----------------------------------------------------------------------------------------
// Local functions

static UINT32 HandleBack( EVENT_STACK_T * ev_st, void * app );


// -----------------------------------------------------------------------------------------
// Global handlers array

const EVENT_HANDLER_ENTRY_T viewer_state_handlers[] =
{
	{ EV_DONE,					HandleBack },

	{ STATE_HANDLERS_END,		NULL }
};

// -----------------------------------------------------------------------------------------
// Global enter/exit state functions

UINT32 ViewerStateEnter( EVENT_STACK_T * ev_st, void * app, ENTER_STATE_TYPE_T type )
{
	NEWAPP_T *		papp = newapp(app);
	SU_PORT_T		port = papp->apt.port;
	UIS_DIALOG_T	dialog;
	CONTENT_T		cnt;
	UINT32			status;
	
	dbg("ViewerStateEnter: Enter, type = %d", type);
	if ( type != ENTER_STATE_ENTER )
		return RESULT_OK;
	
	// TODO: add your code here
	
	status = UIS_MakeContentFromString( "q0Nq1", &cnt, str_view_title, str_view_hw );
	dbg("ViewerStateEnter: UIS_MakeContentFromString DONE, status = %d", status);
	
	if ( status != RESULT_OK )
		return status;
	
	dialog = UIS_CreateViewer( &port, &cnt, NULL );
	dbg("ViewerStateEnter: UIS_CreateViewer DONE, dialog = %d", dialog);
	
	if ( dialog == 0 )
		return RESULT_FAIL;
	papp->apt.dialog = dialog;
	
	dbg("ViewerStateEnter: END", 0);
	return RESULT_OK;
}

UINT32 ViewerStateExit( EVENT_STACK_T * ev_st, void * app, EXIT_STATE_TYPE_T type )
{
	NEWAPP_T *		papp = newapp(app);
	UINT32			status;
	
	dbg("ViewerStateExit: Enter, type = %d", type);
	if ( type != EXIT_STATE_EXIT )
		return RESULT_OK;
	
	status = APP_UtilUISDialogDelete( &papp->apt.dialog );
	
	dbg("ViewerStateExit: END, status = %d", status);
	return status;
}


// -----------------------------------------------------------------------------------------
// Other local functions

UINT32 HandleBack( EVENT_STACK_T * ev_st, void * app )
{
	NEWAPP_T *		papp = newapp(app);
	UINT32			status;
	
	dbg("HandleBack: Enter", 0);
	APP_ConsumeEv(ev_st, app);
	
	// TODO: add your code here
	
	status = APP_UtilChangeState( BACK_STATE, ev_st, app );
	
	dbg("HandleBack: END, status = %d", status);
	return status;
}


